package com.yidu.DailyFinalTreatment.controller;

import com.yidu.BusinessParameters.entity.Trade;
import com.yidu.DailyFinalTreatment.entity.AssetValuationData;
import com.yidu.DailyFinalTreatment.entity.AssetValuationMjoinS;
import com.yidu.DailyFinalTreatment.mapper.AsseMapper;
import com.yidu.DailyFinalTreatment.service.AsseService;
import com.yidu.StockControl.entity.CashInventory;
import com.yidu.StockControl.entity.SecurityInventory;
import com.yidu.StockControl.service.CashInventoryService;
import com.yidu.StockControl.service.SecurityInventoryService;
import com.yidu.TAManagement.entity.TABusiness;
import com.yidu.Util.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@RestController
public class AsseController {
    //调用资产估值service类
    @Resource
    private AsseService asseService;
    //调用资产估值Dao类
    @Resource
    AsseMapper asseMapper;
    //调用现金应收应付库存service类
    @Resource
    CashInventoryService cashInventoryService;
    //调用证券应收应付库存service类
    @Resource
    SecurityInventoryService securityInventoryService;
    //调用工具类
    @Resource
    Dbutil dbutil;


    @RequestMapping("selectAssetValuationData")
    public HashMap selectAssetValuationData() {
        List<AssetValuationData> assetValuationDataList = asseService.selectTable();
        HashMap assetValuationDataMap = new HashMap();
        assetValuationDataMap.put("code", 0);
        assetValuationDataMap.put("msg", "");
        assetValuationDataMap.put("count", null);
        assetValuationDataMap.put("data", assetValuationDataList);
        System.out.println(assetValuationDataMap);
        return assetValuationDataMap;
    }

    @RequestMapping("selectStockarket")
    public void selectStockarket(String toDay, String arrJson, HttpServletRequest request) {
        HttpSession session = request.getSession();
        String fundId = (String) session.getAttribute("fundId");
        List<AssetValuationMjoinS> assetValuationMjoinS = asseService.selectStockarket(toDay, fundId);;
        List<AssetValuationData> assetValuationDatesList = SysUtil.jsonToArrayList(arrJson, AssetValuationData.class);
        for (AssetValuationData assetValuationDate : assetValuationDatesList) {
            if (assetValuationDate.getAssetValuationType().equals("证券估值增值")) {
                for (AssetValuationMjoinS assetValuationMjoin : assetValuationMjoinS
                ) {
                    SecurityInventory securityAccrualStock = new SecurityInventory();
                    String securityInventoryId = NumberUtil.getNumber(TableIdUtil.SASD, TableNameUtil.SI, TableNameUtil.getTableName("securityInventory"));
                    //证券估值
                    System.out.println("securityInventoryId1111"+securityInventoryId);
                    securityAccrualStock.setSecurityInventoryId(securityInventoryId);
                    securityAccrualStock.setFundId(fundId);
                    securityAccrualStock.setSecurityId((assetValuationMjoin.getSecurityId()));
                    securityAccrualStock.setSecurityInventoryTime(toDay);
                    securityAccrualStock.setSecurityName(assetValuationMjoin.getSecurityName());
                    securityAccrualStock.setBusinessStatu(1);
                    securityAccrualStock.setSecurityType(1);
                    securityAccrualStock.setCashId("C20220628001");
                    securityAccrualStock.setSecurityTotalAmount(assetValuationMjoin.getTootaIPrice());
                    //securityAccrualStock.setInitialState(assetValuationMjoin.getInitialState());
                    //securityAccrualStock.setRemark("");
                    int i = asseService.delectSecurityAccrualStock(securityAccrualStock);
                    System.out.println(i+"我是删除");
                        securityInventoryService.insertSecurityInventory(securityAccrualStock);
                        System.out.println("成功");
                }
            } else {
                System.out.println("=====清算款=====");
                List<Trade> dealData = asseService.selectTransactionData(toDay);
                for (Trade DealDatalist : dealData
                ) {
                    SecurityInventory securityAccrualStock = new SecurityInventory();
                    String securityInventoryId = NumberUtil.getNumber(TableIdUtil.CASID, TableNameUtil.CI, TableNameUtil.getTableName("cashInventory"));
                    securityAccrualStock.setSecurityInventoryId(securityInventoryId);
                    securityAccrualStock.setFundId(fundId);
                    securityAccrualStock.setSecurityId(DealDatalist.getSecurityId());
                    securityAccrualStock.setSecurityInventoryTime(toDay);
                    securityAccrualStock.setBusinessStatu(1);
                    securityAccrualStock.setSecurityType(2);
                    securityAccrualStock.setSecurityName(DealDatalist.getSecurityName());
                    securityAccrualStock.setCashId("C20220628001");
                    securityAccrualStock.setSecurityTotalAmount(DealDatalist.getSettleMoney());
                    System.out.println("wenxinyu"+securityAccrualStock);
                    //securityAccrualStock.setInitialState(DealDatalist.getDealState());
                    //securityAccrualStock.setRemark(" ");
                    int i = asseService.delectSecurityAccrualStock(securityAccrualStock);
                    System.out.println("删除行数" + i);
                    securityInventoryService.insertSecurityInventory(securityAccrualStock);
                    System.out.println("成功");


                    System.out.println("==========================TA");
                    List<TABusiness> tas = asseService.selectTa(toDay);
                    CashInventory cashAccrualStock = new CashInventory();
                    for (TABusiness ta : tas
                    ) {
                        cashAccrualStock.setCashInventoryId(NumberUtil.getNumber(TableIdUtil.CASID, TableNameUtil.CI,  TableNameUtil.getTableName("cashInventory")));
                        cashAccrualStock.setCashInventoryTime(ta.getBusinessTime());
                        cashAccrualStock.setFundId(fundId);
                        cashAccrualStock.setCashId(ta.getCashId());
                        cashAccrualStock.setCashTotalAmount((double) ta.getTotalMoney());
                        cashAccrualStock.setBusinessType(4);
                        cashAccrualStock.setBusinessStatu(1);
                        //cashAccrualStock.setInitialState(1);
                        //cashAccrualStock.setRemark(" ");

                    }
//                    int i1 = asseService.delectCashAccrualStock(cashAccrualStock);
//                    //System.out.println(i1+"qwe");
                    int i2 = cashInventoryService.insertCashInventory(cashAccrualStock);
                }
            }
        }
    }
}